<template>
  <div class="shopCart-container">
    <div class="goodsList"
         v-for="(item, index) in goodsMsg"
         :key="index">
      <van-checkbox v-model="item.isSelect"
                    @change="changeSelect(item.id, item.isSelect)"></van-checkbox>
      <img :src="item.imgs[0]"
           @click="toGoodsInfo(item._id)">
      <div class="detail">
        <h2>{{item.title}}</h2>
        <div>
          <span>￥{{item.new_price}}</span>
          <van-stepper v-model="item.count"
                       :disable-input="true"
                       min="1"
                       max="5"
                       @change="updateCount($event,item.id)" />
        </div>
      </div>
      <van-icon name="close"
                size="0.5rem"
                @click="delGood(item.id)" />
    </div>

    <van-submit-bar class="van-hairline--top"
                    :price="$store.getters.getGoodsPrice"
                    button-text="结算">
      <van-checkbox v-model="checked"
                    @change="allSelect">全选</van-checkbox>
    </van-submit-bar>
  </div>
</template>

<script>
import { Dialog } from 'vant';

export default {
  data() {
    return {
      goodsMsg: [],
      checked: false
    }
  },
  created() {
    this.getGoodsMsg()

  },
  methods: {
    // 从缓存中获取商品id，获取商品信息
    getGoodsMsg() {
      let goodsMsg = JSON.parse(localStorage.getItem('cartList')) || []
      let idArr = []
      goodsMsg.forEach(item => {
        idArr.push(item.id)
      })
      if (goodsMsg.length == 0) return
      this.axios.get('/goods/getShopCart', { params: { idArr } }).then(res => {
        if (res.data.code == 1) {
          goodsMsg.forEach(item => {
            res.data.goodsMsg.forEach(el => {
              if (item.id == el._id) {
                Object.assign(item, el)
              }
            })
          })
          this.goodsMsg = goodsMsg
          this.$store.commit('editCartList', goodsMsg)
        }
      })
    },
    // 跳转到商品详情
    toGoodsInfo(id) {
      this.$router.push({ name: 'goodsInfo', params: { id } })
    },
    // 改变商品选择状态
    changeSelect(id, val) {
      this.$store.commit('changeSelect', { id, isSelect: val })
    },
    // 改变全选状态
    allSelect() {
      this.$store.commit('allSelect', this.checked)
    },
    // 删除商品
    delGood(id) {
      Dialog.confirm({
        title: '提示',
        message: '是否真的删除？'
      }).then(() => {
        this.$store.commit('delGood', id)
        let index = this.goodsMsg.findIndex(item => item.id == id)
        this.goodsMsg.splice(index, 1)
      }).catch(() => {
        // on cancel
      });
    },
    // 修改商品数量
    updateCount(e, id) {
      this.$store.commit('updateCount', { count: e, id })
    }
  }
}
</script>

<style lang="scss">
.shopCart-container {
  padding-bottom: 1.36rem;
  .goodsList {
    display: flex;
    align-items: center;
    padding: 0 0.3rem;
    margin: 0.5rem 0;
    position: relative;
    img {
      border-radius: 0.2rem;
      width: 2rem;
      height: 2rem;
      margin: 0 0.8rem;
    }
    .detail {
      flex-grow: 1;
      h2 {
        margin: 0;
        padding-bottom: 0.35rem;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        font-size: 0.4rem;
      }
      div {
        display: flex;
        justify-content: space-between;
        span {
          font-size: 0.45rem;
          font-weight: bold;
          color: red;
        }
        input {
          color: #555;
        }
      }
    }
    .van-icon-close {
      position: absolute;
      top: 0;
      right: 0.3rem;
    }
  }
  .van-submit-bar {
    bottom: 1.36rem;
    .van-checkbox {
      margin-left: 0.3rem;
    }
  }
}
</style>